<?php
class ControllerModuleNews extends Controller {
	private $error = array();

	public function index() {
		$this->load->model('catalog/news');
		$this->model_catalog_news->checkNews();
		$this->load->language('module/news');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('setting/setting');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting('news', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getList();
	}

	public function insert() {
		$this->load->language('module/news');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('catalog/news');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->model_catalog_news->addNews($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getForm();
	}

	public function update() {
		$this->load->language('module/news');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('catalog/news');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->model_catalog_news->editNews($this->request->get['news_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getForm();
	}

	public function delete() {
		$this->load->language('module/news');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('catalog/news');
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $news_id) {
				$this->model_catalog_news->deleteNews($news_id);
			}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module');
		}
		$this->getList();
	}

	private function getList() {
		$this->load->model('catalog/news');
		$this->load->language('module/news');
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_left'] = $this->language->get('text_left');
		$this->data['text_right'] = $this->language->get('text_right');
		$this->data['text_homepage'] = $this->language->get('text_homepage');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');		
		$this->data['entry_limit'] = $this->language->get('entry_limit');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_newspage_thumb'] = $this->language->get('entry_newspage_thumb');
		$this->data['entry_newspage_popup'] = $this->language->get('entry_newspage_popup');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_data'] = $this->language->get('tab_data');
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->error['newspage_thumb'])) {
			$this->data['error_newspage_thumb'] = $this->error['newspage_thumb'];
		} else {
			$this->data['error_newspage_thumb'] = '';
		}
		if (isset($this->error['newspage_popup'])) {
			$this->data['error_newspage_popup'] = $this->error['newspage_popup'];
		} else {
			$this->data['error_newspage_popup'] = '';
		}
		$this->document->breadcrumbs = array();
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=common/home',
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=extension/module',
			'text'      => $this->language->get('text_module'),
			'separator' => ' :: '
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=module/news',
			'text'      => $this->language->get('heading_title'),
			'separator' => ' :: '
		);
		$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/news';
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/module';
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=module/news/insert';
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=module/news/delete';
		$this->data['news'] = array();
		$news_total = $this->model_catalog_news->getTotalNews();
		$results = $this->model_catalog_news->getNews();
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=module/news/update&news_id=' . $result['news_id']
			);
			$this->data['news'][] = array(
				'news_id'     => $result['news_id'],
				'title'       => $result['title'],
				'status'		  => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'     => isset($this->request->post['selected']) && in_array($result['news_id'], $this->request->post['selected']),
				'action'      => $action
			);
		}
		if (isset($this->request->post['news_limit'])) {
			$this->data['news_limit'] = $this->request->post['news_limit'];
		} else {
			$this->data['news_limit'] = $this->config->get('news_limit');
		}
		if (isset($this->request->post['news_position'])) {
			$this->data['news_position'] = $this->request->post['news_position'];
		} else {
			$this->data['news_position'] = $this->config->get('news_position');
		}
		if (isset($this->request->post['news_status'])) {
			$this->data['news_status'] = $this->request->post['news_status'];
		} else {
			$this->data['news_status'] = $this->config->get('news_status');
		}
		if (isset($this->request->post['news_sort_order'])) {
			$this->data['news_sort_order'] = $this->request->post['news_sort_order'];
		} else {
			$this->data['news_sort_order'] = $this->config->get('news_sort_order');
		}
		if (isset($this->request->post['newspage_thumb_width'])) {
			$this->data['newspage_thumb_width'] = $this->request->post['newspage_thumb_width'];
		} else {
			$this->data['newspage_thumb_width'] = $this->config->get('newspage_thumb_width');
		}
		if (isset($this->request->post['newspage_thumb_height'])) {
			$this->data['newspage_thumb_height'] = $this->request->post['newspage_thumb_height'];
		} else {
			$this->data['newspage_thumb_height'] = $this->config->get('newspage_thumb_height');
		}
		if (isset($this->request->post['newspage_popup_width'])) {
			$this->data['newspage_popup_width'] = $this->request->post['newspage_popup_width'];
		} else {
			$this->data['newspage_popup_width'] = $this->config->get('newspage_popup_width');
		}
		if (isset($this->request->post['newspage_popup_height'])) {
			$this->data['newspage_popup_height'] = $this->request->post['newspage_popup_height'];
		} else {
			$this->data['newspage_popup_height'] = $this->config->get('newspage_popup_height');
		}
		$this->template = 'module/news.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function getForm() {
		$this->load->model('catalog/news');
		$this->load->language('module/news');
		$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_default'] = $this->language->get('text_default');
    	$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_data'] = $this->language->get('tab_data');
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}
		if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = '';
		}
		$this->document->breadcrumbs = array();
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=common/home',
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=extension/module',
			'text'      => $this->language->get('text_module'),
			'separator' => ' :: '
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=module/news',
			'text'      => $this->language->get('heading_title'),
			'separator' => ' :: '
		);
		if (!isset($this->request->get['news_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/news/insert';
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/news/update&news_id=' . $this->request->get['news_id'];
		}
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=module/news';
		if ((isset($this->request->get['news_id'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$news_info = $this->model_catalog_news->getNewsStory($this->request->get['news_id']);
		}
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		if (isset($this->request->post['news_description'])) {
			$this->data['news_description'] = $this->request->post['news_description'];
		} elseif (isset($this->request->get['news_id'])) {
			$this->data['news_description'] = $this->model_catalog_news->getNewsDescriptions($this->request->get['news_id']);
		} else {
			$this->data['news_description'] = array();
		}
		$this->load->model('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();
		if (isset($this->request->post['news_store'])) {
			$this->data['news_store'] = $this->request->post['news_store'];
		} elseif (isset($news_info)) {
			$this->data['news_store'] = $this->model_catalog_news->getNewsStores($this->request->get['news_id']);
		} else {
			$this->data['news_store'] = array(0);
		}			
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($news_info)) {
			$this->data['keyword'] = $news_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($news_info)) {
			$this->data['status'] = $news_info['status'];
		} else {
			$this->data['status'] = '';
		}
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (isset($news_info)) {
			$this->data['image'] = $news_info['image'];
		} else {
			$this->data['image'] = '';
		}
		$this->load->model('tool/image');
		if (isset($news_info) && $news_info['image'] && file_exists(DIR_IMAGE . $news_info['image'])) {
			$this->data['preview'] = $this->model_tool_image->resize($news_info['image'], 100, 100);
		} else {
			$this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
		$this->template = 'module/news/form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/news')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if (!$this->request->post['newspage_thumb_width'] || !$this->request->post['newspage_thumb_height']) {
			$this->error['newspage_thumb'] = $this->language->get('error_newspage_thumb');
		}
		if (!$this->request->post['newspage_popup_width'] || !$this->request->post['newspage_popup_height']) {
			$this->error['newspage_popup'] = $this->language->get('error_newspage_popup');
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'module/news')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		foreach ($this->request->post['news_description'] as $language_id => $value) {
			if ((strlen($value['title']) < 3) || (strlen($value['title']) > 250)) {
				$this->error['title'][$language_id] = $this->language->get('error_title');
			}
			if (strlen($value['description']) < 3) {
				$this->error['description'][$language_id] = $this->language->get('error_description');
			}
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'module/news')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>
